package com.qf.mybatisplus.function.OCR.test;

import org.json.JSONArray;
import org.json.JSONObject;

class SplitTools {

    /**
     * 拆分图片识别结果
     */
    void getImageResult(JSONObject res) {
        //获取结果的Json对象
        System.out.println("******识别结果******\n");
        JSONArray result = res.getJSONArray("words_result");

        int count = 0;
        for (int i = 0; i < result.length(); i++) {
            JSONObject obj = (JSONObject) result.get(i);
            count++;
            System.out.println("第" + count + "条结果：" + obj.getString("words"));
        }
    }

    void getZsbhResult(JSONObject res) {
        //获取结果的Json对象
        System.out.println("******识别结果******\n");
        JSONArray result = res.getJSONArray("words_result");
        JSONObject o = (JSONObject) result.get(2);
        String  zsbhStr = (String) o.get("words");

        String zsbh = zsbhStr.substring(4);
        System.err.println("证书编号:"+zsbh);

    }

    /**
     * 拆分银行卡格式
     */
    void getBankCard(JSONObject res) {
//
//        res = "{\n" +
//                "\"result\":{\n" +
//                "\"valid_date\":\"06/31\",\n" +  过期时间 month/year
//                "\"bank_card_number\":\"6236 6815 4002 4467 888\",\n" +  银行卡号
//                "\"bank_name\":\"中国建设银行\",\n" +   银行名称
//                "\"bank_card_type\":1,\n" +    银行卡类型
//                "\"holder_name\":\"\"\n" +    持卡人姓名
//                "},\n" +
//                "\"log_id\":1473498550714864958,\n" +
//                "\"direction\":2\n" +
//                "}";
        //获取识别结果
        System.out.println("******识别结果******\n");
        JSONObject obj = res.getJSONObject("result");

        //获取银行卡信息
        System.out.print("银行名称：");
        String name = obj.getString("bank_name");
        System.out.println(name);

        //获取银行卡类别  银行卡类型，0：不能识别; 1：借记卡; 2：贷记卡（原信用卡大部分为贷记卡）; 3：准贷记卡; 4：预付费卡
        System.out.print("银行卡类别：");
        String cardType = "";
        Integer bankCardType = obj.getInt("bank_card_type");
        if (bankCardType==0){
            cardType = "不能识别";
        }else if(bankCardType==1){
            cardType = "借记卡";
        }else if(bankCardType==2){
            cardType = "贷记卡";
        }else if(bankCardType==3){
            cardType = "准贷记卡";
        }else if(bankCardType==4){
            cardType = "预付费卡";
        }
        System.out.println(cardType);

        //获取银行卡号
        System.out.print("银行卡号：");
        String number = obj.getString("bank_card_number");
        System.out.println(number);
    }

    /**
     * 拆分身份证格式
     */
    void getIDCard(JSONObject res) {

        //获取json数据
        System.out.println("*****识别结果*****\n");
        JSONObject obj = res.getJSONObject("words_result");

        //获取姓名
        System.out.print("姓   名：");
        JSONObject name = obj.getJSONObject("姓名");
        String nameWords = name.getString("words");
        System.out.println(nameWords);

        //获取性别
        System.out.print("性   别：");
        JSONObject sex = obj.getJSONObject("性别");
        String sexWorlds = sex.getString("words");
        System.out.println(sexWorlds);

        //获取民族
        System.out.print("民   族：");
        JSONObject mz = obj.getJSONObject("民族");
        String mzWords = mz.getString("words");
        System.out.println(mzWords);

        //获取出生年月
        System.out.print("出生年月：");
        JSONObject age = obj.getJSONObject("出生");
        String ageWords = age.getString("words");
        System.out.println(ageWords);

        //获取公民身份号码
        System.out.print("身份号码：");
        JSONObject idCard = obj.getJSONObject("公民身份号码");
        String idCardWords = idCard.getString("words");
        System.out.println(idCardWords);

        //获取住址
        System.out.print("家庭住址：");
        JSONObject address = obj.getJSONObject("住址");
        String addressWords = address.getString("words");
        System.out.println(addressWords);
    }

    /**
     * 拆分银行卡格式
     */
    void getUrlImage(JSONObject res) {
        System.out.println("******识别结果******\n");
        JSONArray urlWorld = res.getJSONArray("words_result");

        int count = 0;
        for (int i = 0; i < urlWorld.length(); i++) {
            JSONObject json = (JSONObject) urlWorld.get(i);
            count++;
            System.out.println("Result" + count + ":" + json.getString("words"));
        }
    }
}
